# -*- coding: utf-8 -*-

from datetime import datetime
from openerp import models
from openerp.osv import osv, fields, orm

#扩展电话记录默认值，默认沟通方式为我方去电（数据库ID为:10）
class crm_phonecall_ext(osv.osv):
    """  Model for CRM phonecalls ext  """
    _inherit = 'crm.phonecall'

    _defaults = {
        'categ_id': 10
    }

    #重载电话记录的新建方法，自动填入业务伙伴公司的最后联系时间信息
    def create(self, cr, user, vals, context=None):
        if vals['partner_id'] and vals['date']:
            partner_id = vals['partner_id']
            #获取联系人信息
            partner = self.pool['res.partner'].browse(cr, 1, partner_id, context=context)
            company = partner
            #如果是联系人，找到其公司并修改其最后联系联系时间
            if not partner.is_company:
                company = partner.parent_id
            company.write({'last_contact_time': vals['date']})
        return super(crm_phonecall_ext, self).create(cr, user, vals, context)